﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Properties;
using AnimaGestionPersonnage.Utilities;
using System.Collections;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormCreateWeapon : Form
    {
        private bool saved = true;
        private Weapon thisWeapon = new Weapon();

        public FormCreateWeapon()
        {
            InitializeComponent();
        }

        public void ShowDialog(Form parent)
        {
            this.comboBoxAttMode1.Items.AddRange(new ArrayList(Settings.Default.DamageTypes).ToArray());
            this.comboBoxAttMode2.Items.AddRange(new ArrayList(Settings.Default.DamageTypes).ToArray());
            this.checkedListBoxTypes.Items.AddRange(new ArrayList(Settings.Default.WeaponTypes).ToArray());
            this.checkedListBoxSpecial.Items.AddRange(new ArrayList(Settings.Default.WeaponSpecials).ToArray());

            base.ShowDialog(parent);
        }

        private void textBoxWeaponName_TextChanged(object sender, EventArgs e)
        {
            this.thisWeapon.name = this.textBoxWeaponName.Text;
            this.dataChanged();
        }

        private void numericUpDownDamage_ValueChanged(object sender, EventArgs e)
        {
            this.thisWeapon.damage = (int)this.numericUpDownDamage.Value;
            this.dataChanged();
        }

        private void numericUpDownSpeed_ValueChanged(object sender, EventArgs e)
        {
            this.thisWeapon.speed = (int)numericUpDownSpeed.Value;
            this.dataChanged();
        }

        private void numericUpDownReqStr1H_ValueChanged(object sender, EventArgs e)
        {
            if (this.thisWeapon.reqStr.Count == 0)
            {
                this.thisWeapon.reqStr.Add((int)this.numericUpDownReqStr1H.Value);
            }
            else
            {
                this.thisWeapon.reqStr[0] = (int)this.numericUpDownReqStr1H.Value;
            }
            this.dataChanged();
        }

        private void numericUpDownReqStr2H_ValueChanged(object sender, EventArgs e)
        {
            if (this.thisWeapon.reqStr.Count == 0)
            {
                this.thisWeapon.reqStr.Add((int)this.numericUpDownReqStr1H.Value);
                this.thisWeapon.reqStr.Add((int)this.numericUpDownReqStr2H.Value);
            }
            else if (this.thisWeapon.reqStr.Count == 1)
            {
                this.thisWeapon.reqStr.Add((int)this.numericUpDownReqStr2H.Value);
            }
            else
            {
                this.thisWeapon.reqStr[1] = (int)this.numericUpDownReqStr2H.Value;
            }
            this.dataChanged();
        }

        private void comboBoxAttMode1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.thisWeapon.modes.Count == 0)
            {
                this.thisWeapon.modes.Add((DamageTypes)this.comboBoxAttMode1.SelectedIndex);
            }
            else
            {
                this.thisWeapon.modes[0] = (DamageTypes)this.comboBoxAttMode1.SelectedIndex;
            }
            this.dataChanged();
        }

        private void comboBoxAttMode2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.thisWeapon.modes.Count == 0)
            {
                this.thisWeapon.modes.Add((DamageTypes)this.comboBoxAttMode1.SelectedIndex);
                this.thisWeapon.modes.Add((DamageTypes)this.comboBoxAttMode2.SelectedIndex);
            }
            else if (this.thisWeapon.modes.Count == 1)
            {
                this.thisWeapon.modes.Add((DamageTypes)this.comboBoxAttMode2.SelectedIndex);
            }
            else
            {
                this.thisWeapon.modes[1] = (DamageTypes)this.comboBoxAttMode2.SelectedIndex;
            }
            this.dataChanged();
        }

        private void checkedListBoxTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!this.thisWeapon.types.Contains((WeaponTypes)this.checkedListBoxTypes.SelectedIndex))
            {
                this.thisWeapon.types.Add((WeaponTypes)this.checkedListBoxTypes.SelectedIndex);
            }
            else
            {
                this.thisWeapon.types.Remove((WeaponTypes)this.checkedListBoxTypes.SelectedIndex);
            }
            this.dataChanged();
        }

        private void checkedListBoxSpecial_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!this.thisWeapon.special.Contains((WeaponSpecials)this.checkedListBoxSpecial.SelectedIndex))
            {
                this.thisWeapon.special.Add((WeaponSpecials)this.checkedListBoxSpecial.SelectedIndex);
            }
            else
            {
                this.thisWeapon.special.Remove((WeaponSpecials)this.checkedListBoxSpecial.SelectedIndex);
            }
            this.dataChanged();
        }

        private void numericUpDownFortitude_ValueChanged(object sender, EventArgs e)
        {
            this.thisWeapon.fortitude = (int)this.numericUpDownFortitude.Value;
            this.dataChanged();
        }

        private void numericUpDownBreakage_ValueChanged(object sender, EventArgs e)
        {
            this.thisWeapon.breakage = (int)this.numericUpDownBreakage.Value;
            this.dataChanged();
        }

        private void numericUpDownPresence_ValueChanged(object sender, EventArgs e)
        {
            this.thisWeapon.presence = (int)this.numericUpDownPresence.Value;
            this.dataChanged();
        }

        private void textBoxNotes_TextChanged(object sender, EventArgs e)
        {
            this.thisWeapon.notes = this.textBoxNotes.Text;
            this.dataChanged();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (Utils.weaponList.Contains(this.thisWeapon))
            {
                Utils.weaponList.Remove(this.thisWeapon);
            }
            Utils.weaponList.Add(this.thisWeapon);
            Utils.weaponList.Sort();
            Utils.xmlSerializeWeapons(Settings.Default.FilePath + Settings.Default.WeaponFile);
            saved = true;
            buttonSave.Enabled = false;
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            this.thisWeapon = new FormEditionSelection().ShowDialog(this);
            if (this.thisWeapon != null)
            {
                this.loadWeapon();
                saved = true;
                buttonSave.Enabled = false;
            }
        }

        private void buttonQuit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormCreateWeapon_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this, null,e);
        }

        #region local methods

        private void dataChanged()
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                buttonSave.Enabled = true;
                saved = false;
            }
        }

        private void loadWeapon()
        {
            int tempState = Utils.getCurrentState();

            Utils.setCurrentState(Utils.LOADING_STATE);

            this.textBoxWeaponName.Text = this.thisWeapon.name;
            this.textBoxNotes.Text = this.thisWeapon.notes;
            this.comboBoxAttMode1.SelectedIndex = (int)this.thisWeapon.modes[0];
            this.comboBoxAttMode2.SelectedIndex = (int)this.thisWeapon.modes[1];
            this.numericUpDownBreakage.Value = this.thisWeapon.breakage;
            this.numericUpDownDamage.Value = this.thisWeapon.damage;
            this.numericUpDownFortitude.Value = this.thisWeapon.fortitude;
            this.numericUpDownPresence.Value = this.thisWeapon.presence;
            this.numericUpDownReqStr1H.Value = this.thisWeapon.reqStr[0];
            this.numericUpDownReqStr2H.Value = this.thisWeapon.reqStr[1];
            this.numericUpDownSpeed.Value = this.thisWeapon.speed;

            foreach (int ci in this.checkedListBoxSpecial.CheckedIndices)
            {
                this.checkedListBoxSpecial.SetItemChecked(ci, false);
            }
            foreach (WeaponSpecials ws in this.thisWeapon.special)
            {
                this.checkedListBoxSpecial.SetItemChecked((int)ws, true);
            }

            foreach (int ci in this.checkedListBoxTypes.CheckedIndices)
            {
                this.checkedListBoxTypes.SetItemChecked(ci, false);
            }
            foreach (WeaponTypes wt in this.thisWeapon.types)
            {
                this.checkedListBoxTypes.SetItemChecked((int)wt, true);
            }

            Utils.setCurrentState(tempState);
        }

        #endregion
    }
}
